﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Common;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;
using CourseSystem.Stat.ActionFilter;
using Webdiyer.WebControls.Mvc;

namespace CourseSystem.Web.Controllers
{
    [UserAuth(UserType = UserTypeEnum.Teacher)]
    public class AdminInteractionController : BaseController
    {
        [UrlStatFilter]
        public ActionResult Question()
        {
            return View();
        }

        public ActionResult QuestionListUc(int? pid, string isReply)
        {
            CommonRequest.ClearCache();
            bool isAnswer = isReply == "2";
            var list = QuestionBLL.GetList("", isReply == "0" || string.IsNullOrEmpty(isReply) ? (bool?)null : isAnswer).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        [UploadAuth]
        public ActionResult QuestionEdit(int id)
        {
            var info = QuestionBLL.Get(id);
            if (info == null)
                return NotFound();
            return View(info);
        }

        public ActionResult QuestionDel(int id)
        {
            var info = QuestionBLL.Get(id);
            if (info == null)
                return NotFound();
            if (QuestionBLL.Del(id))
            {
                AttachmentsBLL.DelFileByExpress(o => o.ContentId == info.QuestionContentId);
                SetAlertMsg("删除成功.", true);
            }
            return RedirectToAction("Question");
        }

        [HttpPost]
        [ValidateInput(false)]
        [UploadedAuth]
        public ActionResult QuestionEdit(QuestionInfo info)
        {
            var ran = ViewData["ranStr"].ToString();
            if (info.Id == 0)
                return NotFound();
            ViewData.Model = info;
            if (string.IsNullOrEmpty(info.AnswerContentInfo.DetailContent))
                ModelState.AddModelError("AnswerContentId", "必须填写");
            if (string.IsNullOrEmpty(info.QuestionContentInfo.DetailContent))
                ModelState.AddModelError("QuestionContentId", "必须填写");
            if (!ModelState.IsValid)
                return View();

            var oldInfo = QuestionBLL.Get(info.Id);
            if (oldInfo == null || oldInfo.StudentInfo == null)
                return NotFound();
            oldInfo.QuestionContentInfo.DetailContent = info.QuestionContentInfo.DetailContent;
            oldInfo.AnswerContentInfo.DetailContent = info.AnswerContentInfo.DetailContent;
            if (!oldInfo.IsAnswered)
            {
                oldInfo.IsAnswered = true;
                oldInfo.AnswerId = UserHelper.LoginUser.Id;
                oldInfo.AnswerName = UserHelper.LoginUser.UserName;
                oldInfo.AnswerTime = DateTime.Now;
            }
            oldInfo.IsCommon = info.IsCommon;
            if (CommonContentBLL.Edit(oldInfo.QuestionContentInfo) && CommonContentBLL.Edit(oldInfo.AnswerContentInfo) && QuestionBLL.Edit(oldInfo))
            {
                AttachmentsHelper.ClearUselessAttach(oldInfo.QuestionContentInfo.DetailContent + oldInfo.AnswerContentInfo.DetailContent, ran, oldInfo.QuestionContentInfo.Id);
                SetAlertMsg("编辑成功.");
                return View(oldInfo);
            }
            SetAlertMsg("未知错误.");
            return View();
        }
        [UrlStatFilter]
        public ActionResult Forums()
        {
            return View();
        }

        public ActionResult ForumsListUc(int? pid, string search)
        {
            CommonRequest.ClearCache();
            var list = ForumsBLL.GetList(search, true).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult ForumsEditUc(int? id)
        {
            if (id.HasValue)
            {
                var info = ForumsBLL.Get(id.Value);
                if (info == null)
                {
                    return NotFound();
                }
                return PartialView(info);
            }
            return PartialView(new ForumsInfo { });
        }

        [HttpPost]
        public ActionResult ForumsEditUc(ForumsInfo info)
        {
            ViewData.Model = info;
            if (!ModelState.IsValid)
                return PartialView();
            if (info.Id != 0)
            {
                var oldInfo = ForumsBLL.Get(info.Id);
                if (oldInfo == null)
                    return NotFound();
                oldInfo.Name = info.Name;
                oldInfo.Status = info.Status;
                if (ForumsBLL.Edit(info))
                {
                    SetHighLineMsg("编辑成功.");
                    return PartialView(oldInfo);
                }
                SetHighLineMsg("未知错误.");
                return PartialView();
            }
            var addInfo = new ForumsInfo
                              {
                                  Name = info.Name,
                              };
            if (ForumsBLL.Add(addInfo))
            {
                SetHighLineMsg("编辑成功.");
                return PartialView(addInfo);
            }
            SetHighLineMsg("未知错误.");
            return PartialView();
        }

        public ActionResult ForumsDel(int id)
        {
            var toDelPostList = PostsBLL.GetListByFormId(id).Select(o => o.Id).ToList();
            if (ForumsBLL.Del(id))
            {
                PostsAttachBLL.DelFileByExpress(o => toDelPostList.Contains(o.PostId));
                SetAlertMsg("删除成功！", true);
                return RedirectToAction("Forums");
            }
            SetAlertMsg("未知错误！", true);
            return RedirectToAction("Forums");
        }

    }
}
